/*
  token数组转化成dom字符串 
 */
import lookup from "./lookup";
export default function renderTemplate(tokens, data) {
    var templateStr = ""
    for (let i = 0; i < tokens.length; i++) {
        const token = tokens[i];
        if (token[0] == 'text') {
            templateStr += token[1]
        } else if (token[0] == 'name') {
            templateStr += lookup(token[1], data)
        } else if (token[0] == '#') {
            templateStr += deepArray(token[1], token[2], data)
        }
    }
    return templateStr
}

//递归
function deepArray(key, tokens, data) {
    //当前数据
    let currentData = data[key]
    let str = ''
    for (let i = 0; i < currentData.length; i++) {
        const item = currentData[i];
        str += renderTemplate(tokens, item)
    }
    return str
}